<?php
/**
 * @version		$Id: visitor.php 518 2010-01-29 14:06:55Z michael $
 * @package		FileZilla - Module 4 OpenCart - Admin Controller
 * @copyright	(C) 2010 mic [ http://osworx.net ]. All Rights Reserved.
 * @author		mic - http://osworx.net
 * @license		http://www.gnu.org/copyleft/gpl.html GNU/GPL
 */

class ControllerModuleVisitor extends Controller
{
	private $error		= array();
	private $_name		= '';
	private $_version	= '1.0.0';

	/**
	 * constructor
	 */
	public function __construct() {
		$this->getName();
	}

	/**
	 * gets the module name out of the class
	 */
	private function getName() {
        $this->_fName	= str_replace( 'ControllerModule', '', get_class( $this ) );
		$this->_name	= strtolower( $this->_fName );
    }

	/**
	 * main function
	 */
	public function index() {
		$this->load->model( 'setting/setting' );
		$this->load->language( 'module/' . $this->_name );

		if( ( $this->request->server['REQUEST_METHOD'] == 'POST' ) && ( $this->validate() ) ) {
			$this->model_setting_setting->editSetting( $this->_name, $this->request->post );

			$this->session->data['success'] = $this->language->get( 'text_success' );

			$this->redirect( $this->url->https( 'extension/module' ) );
		}

		$this->data['module']	= $this->_name;
		$this->data['version']	= $this->_version;
		$this->data['action']	= $this->url->https( 'module/' . $this->_name );
		$this->data['cancel']	= $this->url->https( 'extension/module' );

		$this->getLanguage();
		$this->getParams();
		$this->getTemplate();
		$this->getBreadcrumbs();
		$this->getErrors();
		$this->getFooter();

		$this->document->title = $this->language->get( 'heading_title' );

		$this->response->setOutput( $this->render( true ), $this->config->get( 'config_compression' ) );
	}

	/**
	 * building breadcrumbs
	 */
	private function getBreadcrumbs() {
  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => $this->url->https( 'common/home' ),
       		'text'      => $this->language->get( 'text_home' ),
      		'separator' => false
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => $this->url->https( 'extension/module' ),
       		'text'      => $this->language->get( 'text_module' ),
      		'separator' => ' :: '
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => $this->url->https( 'module/' . $this->_name ),
       		'text'      => $this->language->get( 'heading_title' ),
      		'separator' => ' :: '
   		);
	}

	/**
	 * get language vars
	 */
	private function getLanguage() {
		// standard params
		$this->data['heading_title']	= $this->language->get( 'heading_title' );

		$this->data['text_enabled']		= $this->language->get( 'text_enabled' );
		$this->data['text_disabled']	= $this->language->get( 'text_disabled' );
		// positions
		$this->data['text_left']		= $this->language->get( 'text_left' );
		$this->data['text_right']		= $this->language->get( 'text_right' );

		$this->data['button_save']		= $this->language->get( 'button_save' );
		$this->data['button_cancel']	= $this->language->get( 'button_cancel' );

		$this->data['entry_position']	= $this->language->get( 'entry_position' );
		$this->data['entry_status']		= $this->language->get( 'entry_status' );
		$this->data['entry_sort_order']	= $this->language->get( 'entry_sort_order' );

		// module specific params
		$this->data['entry_theme']		= $this->language->get( 'entry_theme' );
		$this->data['entry_expire']		= $this->language->get( 'entry_expire' );
		$this->data['entry_theme']		= $this->language->get( 'entry_theme' );
		$this->data['entry_th_shopping']= $this->language->get( 'entry_th_shopping' );
		$this->data['entry_th_users']	= $this->language->get( 'entry_th_users' );
	}

	/**
	 * checks if requested language var exists
	 * return value only if exist and is different then variable name
	 * can be used if some new language vars are used which may not included in other language packs
	 *
	 * this check is because OC 1.4.0.0 language class returns the variable name instead of value if not exist
	 * use $this->checkLanguage(...) instead of $this->language->get(...)
	 *
	 * @param string	$var	name of requested variable
	 * @return string/void
	 */
	private function checkLanguage( $var ) {
		$ret = $this->language->get( $var );

		if( $ret != $var) {
			return $ret;
		}else{
			return null;
		}
	}

	/**
	 * get params
	 */
	private function getParams() {
		// standard params
		if( isset( $this->request->post[$this->_name . '_position'] ) ) {
			$this->data[$this->_name . '_position'] = $this->request->post[$this->_name . '_position'];
		}else{
			$this->data[$this->_name . '_position'] = $this->config->get( $this->_name . '_position' );
		}

		if( isset( $this->request->post[$this->_name . '_status'] ) ) {
			$this->data[$this->_name . '_status'] = $this->request->post[$this->_name . '_status'];
		}else{
			$this->data[$this->_name . '_status'] = $this->config->get( $this->_name . '_status' );
		}

		if( isset( $this->request->post[$this->_name . '_sort_order'] ) ) {
			$this->data[$this->_name . '_sort_order'] = $this->request->post[$this->_name . '_sort_order'];
		}else{
			$this->data[$this->_name . '_sort_order'] = $this->config->get( $this->_name . '_sort_order' );
		}

		// module specific params
		if( isset( $this->request->post[$this->_name . '_theme'] ) ) {
			$this->data[$this->_name . '_theme'] = $this->request->post[$this->_name . '_theme'];
		}else{
			$this->data[$this->_name . '_theme'] = $this->config->get( $this->_name . '_theme' );
		}

		if( isset( $this->request->post[$this->_name . '_expire'] ) ) {
			$this->data[$this->_name . '_expire'] = $this->request->post[$this->_name . '_expire'];
		}else{
			$this->data[$this->_name . '_expire'] = $this->config->get( $this->_name . '_expire' );
		}

		$this->data['themes'][] = array( 'val' => 'Shopping',	'text'	=> $this->data['entry_th_shopping'] );
		$this->data['themes'][] = array( 'val' => 'Users',		'text'	=> $this->data['entry_th_users'] );
	}

	/**
	 * define template params
	 */
	private function getTemplate() {
		$this->template = 'module/' . $this->_name . '.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
	}

	/**
	 * get error messages
	 */
	private function getErrors() {
 		if( isset( $this->error['warning'] ) ) {
			$this->data['error_warning'] = $this->error['warning'];
		}else{
			$this->data['error_warning'] = '';
		}

 		if( isset( $this->error['expire'] ) ) {
			$this->data['error_expire'] = $this->error['expire'];
		}else{
			$this->data['error_expire'] = '';
		}
	}

	/**
	 * constructs the footer
	 *
	 * Note: displaying this footer is mandatory, removing violates the license!
	 * If you do not want to display the footer, contact the author.
	 */
	private function getFooter() {
		$this->data['oxfooter']	= '<div style="text-align:center; color:#666666; margin-top:5px">'
		. $this->_fName
		. ' Module v.' . $this->_version
		. ' &copy; '
		. date( 'Y' )
		. ' by <a href="http://osworx.net" target="_blank">OSWorX</a>'
		. '</div>'
		;
	}

	/**
	 * validates user permission and checks specific module vars
	 */
	private function validate() {
		if( !$this->user->hasPermission( 'modify', 'module/' . $this->_name ) ) {
			$this->error['warning'] = $this->language->get( 'error_permission' );
		}

		if( !$this->request->post[$this->_name . '_expire'] ) {
			$this->error['expire'] = $this->language->get( 'error_expire' );
		}

		if( !$this->error ) {
			return true;
		}else{
			return false;
		}
	}
}